package com.videoplayer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.db.DBConnect;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.UMFeedbackService;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.util.ConfirmDialog;
import com.util.MyToast;

public class SettingActivity extends Activity implements OnClickListener {
	private final String DB_GET_SHARPNESS = "name = 'sharpness'";
	private final String DB_GET_QUICK = "name = 'quick_resolve'";
	private final String DB_GET_WIFI = "name = 'wifi_only'";
	private final String COLUME_QUICK = "quick_resolve";
	private final String COLUME_WIFI = "wifi_only";
	private int sharpness;
	private int quick;
	private int wifi;
	private RelativeLayout rlDownloadManger;
	private RelativeLayout rlQuick;
	private ImageView ivQuick;
	private boolean quickFlag;
	private RelativeLayout rlWifiOnly;
	private ImageView ivWifiOnly;
	private boolean wifiFlag;
	private RelativeLayout rlSharpness;
	private TextView tvSharpness;
	private RelativeLayout rlAdvices;
	private RelativeLayout rlAboutUs;
	private RelativeLayout rlCheckUpdate;
	private Button btnBack;
	private DBConnect dbCon;
	private int activityResult = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.onError(this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.setting_actitvity);
		dbCon = new DBConnect(this);
		rlDownloadManger = (RelativeLayout) findViewById(R.id.rl_download_manger);
		rlQuick = (RelativeLayout) findViewById(R.id.rl_quick_resolve);
		ivQuick = (ImageView) findViewById(R.id.iv_cb_quick);
		rlWifiOnly = (RelativeLayout) findViewById(R.id.rl_wifi_only);
		ivWifiOnly = (ImageView) findViewById(R.id.iv_cb_wifi);
		rlSharpness = (RelativeLayout) findViewById(R.id.rl_sharpness);
		tvSharpness = (TextView) findViewById(R.id.tv_sharp_content);
		rlAdvices = (RelativeLayout) findViewById(R.id.rl_divices);
		rlAboutUs = (RelativeLayout) findViewById(R.id.rl_about_us);
		rlCheckUpdate = (RelativeLayout) findViewById(R.id.rl_update);
		btnBack = (Button) findViewById(R.id.btn_setting_back);
		rlDownloadManger.setOnClickListener(this);
		rlQuick.setOnClickListener(this);
		rlWifiOnly.setOnClickListener(this);
		rlSharpness.setOnClickListener(this);
		rlAdvices.setOnClickListener(this);
		rlAboutUs.setOnClickListener(this);
		rlCheckUpdate.setOnClickListener(this);
		btnBack.setOnClickListener(this);

	}

	UmengUpdateListener updateSetListener = new UmengUpdateListener() {
		@Override
		public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
			switch (updateStatus) {
			case 0: // has update
				Log.i("--->", "callback result");
				UmengUpdateAgent.showUpdateDialog(SettingActivity.this,
						updateInfo);
				break;
			case 1: // has no update
				MyToast toast = new MyToast(SettingActivity.this, "没有更新", 1500);
				toast.show();
				break;
			case 2: // none wifi
				MyToast toast2 = new MyToast(SettingActivity.this,
						"没有wifi连接， 只在wifi下更新", 1500);
				toast2.show();
				break;
			case 3: // time out
				MyToast toast3 = new MyToast(SettingActivity.this, "超时", 1500);
				toast3.show();
				break;
			}

		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		dbCon.open();
		sharpness = dbCon.queryOneData(DB_GET_SHARPNESS);
		quick = dbCon.queryOneData(DB_GET_QUICK);
		wifi = dbCon.queryOneData(DB_GET_WIFI);
		dbCon.close();
		if (0 == quick) {
			settingItemUncheck(ivQuick);
			quickFlag = false;
		} else {
			settingItemChecked(ivQuick);
			quickFlag = true;
		}
		if (0 == wifi) {
			settingItemUncheck(ivWifiOnly);
			wifiFlag = false;
		} else {
			settingItemChecked(ivWifiOnly);
			wifiFlag = true;
		}
		switch (sharpness) {
		case 0:
			tvSharpness.setText("标清");
			break;
		case 1:
			tvSharpness.setText("高清");
			break;
		case 2:
			tvSharpness.setText("超清");
			break;
		}
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void settingItemChecked(ImageView r) {
		r.setBackgroundResource(R.drawable.focused);
	}

	private void settingItemUncheck(ImageView r) {
		r.setBackgroundResource(R.drawable.normal);
	}

	private long setDBValue(String name, int value) {
		dbCon.open();
		long result = dbCon.updateValue(name, value);
		dbCon.close();
		return result;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent aintent = new Intent(SettingActivity.this,
					MainActivity.class);
			setResult(activityResult, aintent);
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_download_manger:
			Intent intentDown = new Intent();
			intentDown.setClass(SettingActivity.this, DownLoadActivity.class);

			startActivity(intentDown);
			break;
		case R.id.rl_quick_resolve:
			if (quickFlag) {
				settingItemUncheck(ivQuick);
				quickFlag = false;
				setDBValue(COLUME_QUICK, 0);
			} else {
				final ConfirmDialog dialog = new ConfirmDialog(SettingActivity.this,
						R.style.MyDialog, "开启快速解析后将只提供默认清晰度选项，并不会解析视频集数列表", 0);
				dialog.show();
				Button btnOk = (Button) dialog
						.findViewById(R.id.dialog_confirm_button_ok);
				btnOk.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						settingItemChecked(ivQuick);
						quickFlag = true;
						setDBValue(COLUME_QUICK, 1);
					}
				});
				Button btnCancel = (Button) dialog
						.findViewById(R.id.dialog_confirm_button_cancel);
				btnCancel.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
			}
			break;
		case R.id.rl_wifi_only:
			if (wifiFlag) {
				settingItemUncheck(ivWifiOnly);
				wifiFlag = false;
				setDBValue(COLUME_WIFI, 0);
				activityResult = -1;
			} else {
				settingItemChecked(ivWifiOnly);
				wifiFlag = true;
				setDBValue(COLUME_WIFI, 1);
				activityResult = 0;
			}
			break;
		case R.id.rl_sharpness:
			Intent intent = new Intent();
			intent.setClass(SettingActivity.this,
					SettingSharpnessActivity.class);
			intent.putExtra("sharpness", sharpness);
			startActivity(intent);
			break;
		case R.id.rl_divices:
			UMFeedbackService.openUmengFeedbackSDK(this);
			break;
		case R.id.rl_about_us:
			Intent intentAbout = new Intent();
			intentAbout.setClass(SettingActivity.this, AboutActivity.class);
			startActivity(intentAbout);
			break;
		case R.id.rl_update:
			UmengUpdateAgent.update(this);
			com.umeng.common.Log.LOG = true;
			UmengUpdateAgent.setUpdateAutoPopup(false);
			UmengUpdateAgent.setUpdateOnlyWifi(false);
			UmengUpdateAgent.setUpdateListener(updateSetListener);
			break;
		case R.id.btn_setting_back:
			Intent aintent = new Intent(SettingActivity.this,
					MainActivity.class);
			setResult(activityResult, aintent);
			this.finish();
			break;
		default:
			break;
		}
	}

}
